package vue;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JMenuBar;

import java.awt.FlowLayout;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.BoxLayout;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;

import metier.dao.IDAO;
import metier.dao.LieuDao;
import metier.dao.ParcoursDao;
import metier.dao.PoiDao;

import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

 /* Create the frame.
	 */

	public class Accueil extends JFrame {

		private JPanel contentPane;
		private JTable table;
		private JTable table_1;
		private JTable table_2;
		private JTextField textFieldLibelleAccueil;
		private JScrollPane scrollPane;
		private JPanel panelDroiteAccueil;
		
		ResultSet resultatLieu = null;
		ResultSet resultatParcours = null;
		ResultSet resultatPoi = null;
		
		/**
		 * Launch the application.
		 */
		public static void main(String[] args) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						Accueil frame = new Accueil();
						frame.setVisible(true);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}

		public void close()
		{
			this.dispose();
		}
		public void refreshPage()
		{
			this.repaint();
		}
		/**
		 * Create the frame.
		 */

		public Accueil() {
			
			
			
			setTitle("Mini SIG");
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 100, 1015, 672);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane);
			
			JPanel menuPanel = new JPanel();
			menuPanel.setLayout(new BorderLayout(0, 0));
			
			JMenuBar menuBar = new JMenuBar();
			menuPanel.add(menuBar);
			
			JButton btnNewButton = new JButton("Accueil");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					refreshPage();
				}
			});
			menuBar.add(btnNewButton);

			
			JButton btnNewButton_1 = new JButton("Consulter");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) { //bouton consulter
					Consulter consulterVue = new Consulter();
					consulterVue.setVisible(true);
				//((JFrame) consultervue.getTopLevelAncestor()).dispose() ; 
					//setDefaultCloseOperation(DISPOSE_ON_CLOSE);
					close();
				}
			});
			menuBar.add(btnNewButton_1);
			
			JButton btnNewButton_2 = new JButton("Editer");
			btnNewButton_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Editer editerVue = new Editer();
					editerVue.setVisible(true);
					close();
				}
			});
			menuBar.add(btnNewButton_2);
			
			JPanel panelGeneralAccueil = new JPanel();
			panelGeneralAccueil.setAutoscrolls(true);
			panelGeneralAccueil.setLayout(null);
			
			JPanel panelGaucheAccueil = new JPanel();
			panelGaucheAccueil.setBounds(0, 0, 463, 581);
			panelGeneralAccueil.add(panelGaucheAccueil);
			panelGaucheAccueil.setLayout(null);
			
			JLabel lblNews = new JLabel("News");
			lblNews.setBounds(134, 90, 97, 25);
			panelGaucheAccueil.add(lblNews);
			lblNews.setFont(new Font("Tahoma", Font.PLAIN, 20));
			
			JTabbedPane tabbedPaneAccueil = new JTabbedPane(JTabbedPane.TOP);
			tabbedPaneAccueil.setBounds(61, 126, 246, 280);
			tabbedPaneAccueil.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
			tabbedPaneAccueil.setToolTipText("");
			panelGaucheAccueil.add(tabbedPaneAccueil);
			
			// -------- Lieu --------
			JPanel panelLieuAccueil = new JPanel();
			tabbedPaneAccueil.addTab("Lieu", null, panelLieuAccueil, null);
			panelLieuAccueil.setLayout(null);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			scrollPane.setBounds(0, 0, 241, 252);
			panelLieuAccueil.add(scrollPane);
			
			table = new JTable();
			table.addFocusListener(new FocusAdapter() {
				@Override
				public void focusGained(FocusEvent arg0) {
					//String tableSelect = 
				}
			});
			table.setModel(new DefaultTableModel(new Object[][] {null}, new String[] {"Lieu"}));
			scrollPane.setViewportView(table);

			String col[] = { "Lieu" };
		    String cont[][] = new String[10][1];
			
			IDAO dao = (IDAO) new LieuDao();
			resultatLieu = dao.lister();
		      
		      int i = 0;
		      try {
				while (resultatLieu.next()) 
				  {
				        String currentLibelle = resultatLieu.getString("LIBELLELIEU");
				        cont[i][0] = currentLibelle;
				        i++;
				  }
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		      table.setModel(new DefaultTableModel(cont, col));
		      table.setShowGrid(true);
		      table.setShowVerticalLines(true);
		      
			
			// -------- POI --------
			JPanel panelPOIAccueil = new JPanel();
			tabbedPaneAccueil.addTab("Point d'inter\u00EAt", null, panelPOIAccueil, null);
			panelPOIAccueil.setLayout(null);
			
			
			JScrollPane scrollPane_2 = new JScrollPane();
			scrollPane_2.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			scrollPane_2.setBounds(0, 0, 241, 252);
			panelPOIAccueil.add(scrollPane_2);
			
			table_1 = new JTable();
			table_1.setModel(new DefaultTableModel(new Object[][] {null}, new String[] {"Point d'inter\u00EAt"}));
			scrollPane_2.setViewportView(table_1);

			String col_1[] = { "Point d'inter\u00EAt" };
		    String cont_1[][] = new String[10][1];
			
			IDAO dao_1 = (IDAO) new PoiDao();
			resultatPoi = dao_1.lister();
		      
		      int j = 0;
		      try {
				while (resultatPoi.next()) 
				  {
				        String currentLibelle = resultatPoi.getString("LIBELLEPOI");
				        cont_1[j][0] = currentLibelle;
				        j++;
				  }
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		      table_1.setModel(new DefaultTableModel(cont_1, col_1));
		      table_1.setShowGrid(true);
		      table_1.setShowVerticalLines(true);
		      
			// -------- Parcours --------		
			JPanel panelParcoursAccueil = new JPanel();
			tabbedPaneAccueil.addTab("Parcours", null, panelParcoursAccueil, null);
			panelParcoursAccueil.setLayout(null);
			
			JScrollPane scrollPane_1 = new JScrollPane();
			scrollPane_1.setBounds(0, 0, 241, 252);
			panelParcoursAccueil.add(scrollPane_1);
			
			table_2 = new JTable();
			table_2.setModel(new DefaultTableModel(new Object[][] {null}, new String[] {"Parcours"}));
			scrollPane_1.setViewportView(table_2);

			String col_2[] = { "Parcours" };
		    String cont_2[][] = new String[10][1];
			
			IDAO dao_2 = (IDAO) new ParcoursDao();
			resultatParcours = dao_2.lister();

		      int k = 0;
		      try {
				while (resultatParcours.next()) 
				  {
				        String currentPoi = resultatParcours.getString("LIBELLEPARCOURS");
				        cont_2[k][0] = currentPoi;
				        k++;
				  }
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		      table_2.setModel(new DefaultTableModel(cont_2, col_2));
		      table_2.setShowGrid(true);
		      table_2.setShowVerticalLines(true);
		      
			
			panelDroiteAccueil = new JPanel();
			panelDroiteAccueil.setVisible(false);
			panelDroiteAccueil.setAlignmentY(Component.BOTTOM_ALIGNMENT);
			panelDroiteAccueil.setAlignmentX(Component.RIGHT_ALIGNMENT);
			panelDroiteAccueil.setBorder(new LineBorder(new Color(0, 0, 0), 2));
			panelDroiteAccueil.setBounds(473, 11, 370, 570);
			panelGeneralAccueil.add(panelDroiteAccueil);
			panelDroiteAccueil.setLayout(null);
			
			JButton btnConsulterAccueil = new JButton("Consulter");
			btnConsulterAccueil.setBounds(271, 536, 89, 23);
			panelDroiteAccueil.add(btnConsulterAccueil);
			
			JLabel lblNewLabelLibelleAccueil = new JLabel("Libell\u00E9");
			lblNewLabelLibelleAccueil.setBounds(24, 11, 89, 14);
			panelDroiteAccueil.add(lblNewLabelLibelleAccueil);
			
			textFieldLibelleAccueil = new JTextField();
			textFieldLibelleAccueil.setEditable(false);
			textFieldLibelleAccueil.setBounds(24, 29, 86, 20);
			panelDroiteAccueil.add(textFieldLibelleAccueil);
			textFieldLibelleAccueil.setColumns(10);
			
			JLabel lblDescriptifAccueil = new JLabel("Descriptif");
			lblDescriptifAccueil.setBounds(24, 83, 89, 14);
			panelDroiteAccueil.add(lblDescriptifAccueil);
			
			JPanel panelImageAccueil = new JPanel();
			panelImageAccueil.setBounds(24, 272, 336, 210);
			panelDroiteAccueil.add(panelImageAccueil);
			
			JLabel lblImageAccueil = new JLabel("Image");
			lblImageAccueil.setBounds(24, 234, 89, 14);
			panelDroiteAccueil.add(lblImageAccueil);
			
			JTextArea textArea = new JTextArea();
			textArea.setEditable(false);
			textArea.setBounds(24, 108, 223, 93);
			panelDroiteAccueil.add(textArea);
			GroupLayout gl_contentPane = new GroupLayout(contentPane);
			gl_contentPane.setHorizontalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
					.addComponent(menuPanel, GroupLayout.PREFERRED_SIZE, 236, GroupLayout.PREFERRED_SIZE)
					.addComponent(panelGeneralAccueil, GroupLayout.PREFERRED_SIZE, 853, GroupLayout.PREFERRED_SIZE)
			);
			gl_contentPane.setVerticalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_contentPane.createSequentialGroup()
						.addComponent(menuPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(11)
						.addComponent(panelGeneralAccueil, GroupLayout.PREFERRED_SIZE, 581, GroupLayout.PREFERRED_SIZE))
			);
			contentPane.setLayout(gl_contentPane);
		}
}

